package com.zhenzhen.xszhcp.controller;

import com.alibaba.fastjson.JSONObject;
import com.zhenzhen.xszhcp.model.*;
import com.zhenzhen.xszhcp.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.DecimalFormat;
import java.util.*;

@RestController
public class upController {
    @Autowired
    private GroupinfoService groupinfoService;
    @Autowired
    private LogService logService;
    @Autowired
    private MenuService menuService;
    @Autowired
    private TeacherService teacherService;
    @Autowired
    private StudentService studentService;
    @Autowired
    private KechengService kechengService;
    @Autowired
    private ClassesService classesService;
    @Autowired
    private SemesterService semesterService;
    @Autowired
    private ZhuangtaiService zhuangtaiService;
    @Autowired
    private UsersService usersService;
    @Autowired
    private ActivityService activityService;
    @Autowired
    private GradeService gradeService;
    @Autowired
    private ZhcjService zhcjService;
    @Autowired
    private ImportUtilService importUtilService;
    @Autowired
    private KcblService kcblService;

    //修改权限信息
    @RequestMapping("/upyhzqxgl")
    public boolean upyhzqxgl(@RequestParam(value = "id") String id,
                             @RequestParam(value = "checked") String checked,
                             HttpServletRequest httpServletRequest) {
        String ids[] = id.split("_");
        int fcdid=Integer.parseInt(ids[0]);
        int zid = Integer.parseInt(ids[2]);
        String info="增加";
        Groupinfo groupinfo = groupinfoService.getGroupinfoById(zid);
        if (groupinfo != null) {
            String qx = groupinfo.getQx();
            String qxs[] = {};
            if (qx!=null) {
                qxs = qx.split(",");
            }
            if (checked.equals("true"))
            //增加
            {  info="增加";
                String aaaa= Arrays.toString(qxs);
                if (aaaa.length()>2)
                    qx+=","+fcdid;
                else
                    qx=String.valueOf(fcdid);
            }
            if (checked.equals("false"))
            //减少
            {
                qx="";
                info="减少";
                int tem = 0;
                int i;
                for(i = 0; i<qxs.length; i++){
                    if(Integer.parseInt(qxs[i])==fcdid){
                        tem = i;
                    }
                }
                for (i = 0;i<qxs.length;i++)
                {
                    if (i==tem)
                        continue;
                    qx+=qxs[i]+",";
                }
                if (qx.length()>1)
                    qx = qx.substring(0, qx.length() -1);
            }
            groupinfo.setQx(qx);
            if (groupinfoService.upGroupinfo(groupinfo) != 0) {
                setLog.setlod(httpServletRequest, "修改了id为" + zid + "用户组的权限," + info + "权限id" + fcdid,logService);
                return true;
            }
            return false;
        }
        return false;
    }

    /*管理员修改教师信息*/
    @RequestMapping("/upjsxxgl")
    public boolean upjsxxgl(@RequestParam(value = "id") int id,
                            @RequestParam(value = "field") String field,
                            @RequestParam(value = "updateValue") String updateValue,
                            HttpServletRequest httpServletRequest) {
        Teacher teacher = teacherService.getTeacherById(id);
        if (teacher != null) {
            if (field.equals("tname"))
                teacher.setTname(updateValue);
            if (field.equals("tsex"))
                teacher.setTsex(updateValue);
            if (field.equals("tzc"))
                teacher.setTzc(updateValue);
            if (field.equals("tphone"))
                teacher.setTphone(updateValue);
            if (teacherService.upTeacher(teacher) != 0) {
                setLog.setlod(httpServletRequest, "修改了id为" + id + "的教师" + field + "为" + updateValue,logService);
                return true;
            }
            return false;
        }
        return false;
    }

    /*修改班级信息*/
    @RequestMapping("/upbjxxgl")
    public boolean upbjxxgl(@RequestParam(value = "id") int id,
                            @RequestParam(value = "field") String field,
                            @RequestParam(value = "updateValue") String updateValue,
                            HttpServletRequest httpServletRequest) {
        Classes classes = classesService.getClassesById(id);
        if (classes != null) {
            if (field.equals("bj"))
                classes.setBj(updateValue);
            if (field.equals("xy"))
                classes.setXy(updateValue);
            if (field.equals("zy"))
                classes.setZy(updateValue);
            if (field.equals("nj"))
                classes.setNj(updateValue);
            if (classesService.upClasses(classes) != 0) {
                setLog.setlod(httpServletRequest, "修改了id为" + id + "的班级" + field + "为" + updateValue,logService);
                return true;
            }
            return false;
        }
        return false;
    }

    /*修改学期信息*/
    @RequestMapping("/upxqxxgl")
    public boolean upxqxxgl(@RequestParam(value = "id") int id,
                            @RequestParam(value = "field") String field,
                            @RequestParam(value = "updateValue") String updateValue,
                            HttpServletRequest httpServletRequest) {
        Semester semester = semesterService.getSemesterById(id);
        if (semester != null) {
            if (field.equals("xq"))
                semester.setXq(updateValue);
            if (field.equals("year"))
                semester.setYear(updateValue);
            if (semesterService.upSemester(semester) != 0) {
                setLog.setlod(httpServletRequest, "修改了id为" + id + "的学期" + field + "为" + updateValue,logService);
                return true;
            }
            return false;
        }
        return false;
    }

    /*修改状态信息*/
    @RequestMapping("/upztxxgl")
    public boolean upztxxgl(@RequestParam(value = "id") int id,
                            @RequestParam(value = "field") String field,
                            @RequestParam(value = "updateValue") String updateValue,
                            HttpServletRequest httpServletRequest) {
        Zhuangtai zhuangtai = zhuangtaiService.getZhuangtaiById(id);
        if (zhuangtai != null) {
                zhuangtai.setZtname(updateValue);
            if (zhuangtaiService.upZhuangtai(zhuangtai) != 0) {
                setLog.setlod(httpServletRequest, "修改了id为" + id + "的状态" + field + "为" + updateValue,logService);
                return true;
            }
            return false;
        }
        return false;
    }

    /*修改课程信息*/
    @RequestMapping("/upkcxxgl")
    public boolean upkcxxgl(@RequestParam(value = "id") int id,
                            @RequestParam(value = "field") String field,
                            @RequestParam(value = "updateValue") String updateValue,
                            HttpServletRequest httpServletRequest) {
        Kecheng kecheng = kechengService.getKechengById(id);
        if (kecheng != null) {
            if (field.equals("kname"))
                kecheng.setKname(updateValue);
            if (field.equals("zxs"))
                kecheng.setZxs(Integer.parseInt(updateValue));
            if (field.equals("xf"))
                kecheng.setXf(Integer.parseInt(updateValue));
            if (kechengService.upKecheng(kecheng) != 0) {
                setLog.setlod(httpServletRequest, "修改了id为" + id + "的课程" + field + "为" + updateValue,logService);
                return true;
            }
            return false;
        }
        return false;
    }

    /*重置用户密码*/
    @RequestMapping("/inituser")
    public boolean inituser(@RequestParam int id, HttpServletRequest httpServletRequest) {
        Users users = usersService.selectByPrimaryKey(id);
        if (users != null) {
            users.setUpassword(UtilPacket.Md5MD5String("123456"));
            if (usersService.updateByPrimaryKeySelective(users) != 0) {
                setLog.setlod(httpServletRequest, "重置了" + id + "用户的密码",logService);
                return true;
            }
        }
        return false;
    }

     /*教师管理员更改学生信息*/
    @RequestMapping("/upxsxxgl")
    public boolean upxsxxgl(@RequestParam(value = "id") int id,
                            @RequestParam(value = "field") String field,
                            @RequestParam(value = "updateValue") String updateValue,
                            HttpServletRequest httpServletRequest) {
        Student student = studentService.getStudentById(id);
        if (student != null) {
            if (field.equals("sname"))
                student.setSname(updateValue);
            if (field.equals("ssex"))
                student.setSsex(updateValue);
            if (field.equals("sphone"))
                student.setSphone(updateValue);
            if (studentService.upStudent(student) != 0) {
                setLog.setlod(httpServletRequest, "修改了id为" + id + "的学生" + field + "为" + updateValue,logService);
                return true;
            }
            return false;
        }
        return false;
    }

    /*重新核算学生成绩*/
    @RequestMapping("/upxsyycl")
    public boolean upxsyycl(@RequestParam(value = "id") int id,
                            HttpServletRequest httpServletRequest) {
        List<Kcbl> kcbls = kcblService.getkcbllist();
        double dybl = 0.3;
        double tybl = 0.1;
        double stbl = 1;
        double zhbl = 0.6;
        DecimalFormat df = new DecimalFormat("#.00");
        if (kcbls!=null){
            for(Kcbl kcbl:kcbls){
                String bl = kcbl.getBl();
                String name = kcbl.getKname();
                if (name.equals("德育")){
                    dybl = Double.parseDouble(bl);
                }
                if (name.equals("体育")){
                    tybl = Double.parseDouble(bl);
                }
                if (name.equals("素拓")){
                    stbl = Double.parseDouble(bl);
                }
                if (name.equals("综合成绩学分绩点")){
                    zhbl = Double.parseDouble(bl);
                }
            }
        }
        Zhcj zhcj = zhcjService.getZhcjByid(id);
        if (zhcj != null) {
            int sssid = zhcj.getSssid();
            int ssuid = zhcj.getSsuid();
            Map paramMap = new HashMap();
            paramMap.put("sssid",sssid);
            paramMap.put("st",ssuid);
            List<Grade> grades = gradeService.getGradeList(paramMap);
            if (grades!=null){
                double xfjd = 0;
                double st = 0;
                double dy = 0;
                double ty = 0;
                double zcj=0;
                double zxf=0;
                int dkxf=0;
                double zxfjd=0;
                for (Grade grade:grades){
                    int sskid = grade.getSskid();
                    Kecheng kecheng = kechengService.getKechengById(sskid);
                    String kname = "已删除该课程";
                    if (kecheng!=null) {
                        kname = kecheng.getKname();
                        dkxf = kecheng.getXf();
                    }
                    if (kname.equals("素拓")) {
                        st = Double.parseDouble(grade.getGrade())* stbl;
                    }else if (kname.equals("德育")) {
                        dy = Double.parseDouble(grade.getGrade())*dybl;
                    }else if (kname.equals("体育")) {
                        ty = Double.parseDouble(grade.getGrade())*tybl;
                    }else if (kname.equals("已删除该课程")){
                        continue;
                    }
                    else {
                        zcj=zcj+Double.parseDouble(grade.getGrade())*dkxf;
                        zxf=zxf+dkxf;
                        if (zxf!=0)
                            zxfjd=zcj/zxf;
                    }
                }
                String xfjdstr = String.valueOf(df.format(zxfjd));
                String yscjstr = String.valueOf(df.format(zxfjd*zhbl+dy*dybl+ty*tybl));
                String zhcjstr = String.valueOf(df.format(zxfjd*zhbl+dy*dybl+ty*tybl+st*stbl));
                zhcj.setZxfjd(xfjdstr);
                zhcj.setYscj(yscjstr);
                zhcj.setZhcj(zhcjstr);
                zhcj.setSszid(1);
            }
            if (zhcjService.upZhcj(zhcj) != 0) {
                setLog.setlod(httpServletRequest, "重新核算了id为" + id + "成绩",logService);
                return true;
            }
            return false;
        }
        return false;
    }

    /*学生确认成绩*/
    @RequestMapping("/qrcj")
    public boolean qrcj(@RequestParam(value = "id") int id,
                            HttpServletRequest httpServletRequest) {
        Zhcj zhcj = zhcjService.getZhcjByid(id);
        if (zhcj != null) {
            zhcj.setSszid(3);
            if (zhcjService.upZhcj(zhcj) != 0) {
                setLog.setlod(httpServletRequest, "确认了id为" + id + "的成绩",logService);
                return true;
            }
            return false;
        }
        return false;
    }

    /*学生对综合成绩提出异议*/
    @RequestMapping("/tcyy")
    public boolean tcyy(@RequestParam(value = "id") int id,
                        HttpServletRequest httpServletRequest) {
        Zhcj zhcj = zhcjService.getZhcjByid(id);
        if (zhcj != null) {
            zhcj.setSszid(2);
            if (zhcjService.upZhcj(zhcj) != 0) {
                setLog.setlod(httpServletRequest, "id为" + id + "的成绩提出了异议",logService);
                return true;
            }
            return false;
        }
        return false;
    }

    /*学生修改个人信息*/
    @RequestMapping("/upgrxxxgxs")
    public boolean upgrxxxgxs(@RequestParam(value = "id") int id,
                            @RequestParam(value = "field") String field,
                            @RequestParam(value = "updateValue") String updateValue,
                            HttpServletRequest httpServletRequest) {
        Student student = studentService.getStudentById(id);
            //System.out.println(student);
        if (student != null) {
            if (field.equals("sname"))
                student.setSname(updateValue);
            if (field.equals("ssex"))
                student.setSsex(updateValue);
            if (field.equals("sphone"))
                student.setSphone(updateValue);
            if (studentService.upStudent(student) != 0) {
                setLog.setlod(httpServletRequest, "修改了id为" + id + "的学生" + field + "为" + updateValue,logService);
                return true;
            }
            return false;
        }
        return false;
    }

    /*修改学生成绩*/
    @RequestMapping("/upxscjgl")
    public boolean upxscjgl(@RequestParam(value = "id") int id,
                              @RequestParam(value = "field") String field,
                              @RequestParam(value = "updateValue") String updateValue,
                              HttpServletRequest httpServletRequest) {
        Grade grade = gradeService.getGradeById(id);
        if (grade != null) {
            int kid = grade.getSskid();
            if (field.equals("grade")) {
                grade.setGrade(updateValue);
                int xf = kechengService.getKechengById(kid).getXf();
                if(Double.parseDouble(updateValue)>=60){
                    double xfjd = (Integer.parseInt(updateValue)-50)/10;
                        grade.setXfjd(String.valueOf(xfjd));
                    }
               else{
                    grade.setXfjd(String.valueOf(0));
                }
            }
            if (gradeService.upGrade(grade) != 0) {
                setLog.setlod(httpServletRequest, "修改了id为" + id + "的成绩" + field + "为" + updateValue,logService);
                return true;
            }
            return false;
        }
        return false;
    }
    /*修改活动信息*/
    @RequestMapping("/uphdjlgl")
    public boolean uphdjlgl(@RequestParam(value = "id") int id,
                            @RequestParam(value = "field") String field,
                            @RequestParam(value = "updateValue") String updateValue,
                            HttpServletRequest httpServletRequest) {
        Activity activity = activityService.getActivityById(id);
        if (activity != null) {
            if (field.equals("aname"))
                activity.setAname(updateValue);
            if (field.equals("axx"))
                activity.setAxx(updateValue);
            if (field.equals("add"))
                activity.setAdd(updateValue);
            if (field.equals("alx"))
                activity.setAlx(updateValue);
            if (activityService.upActivity(activity) != 0) {
                setLog.setlod(httpServletRequest, "修改了id为" + id + "的活动" + field + "为" + updateValue,logService);
                return true;
            }
            return false;
        }
        return false;
    }
    /*修改密码*/
    @RequestMapping("/setnewpassword")
    public Map setnewpassword(@RequestParam String newpass,
                            @RequestParam String newpass2,
                            HttpServletRequest httpServletRequest) {
        HttpSession session = httpServletRequest.getSession();
        Users users = (Users) session.getAttribute("islogin");
        Map map = new HashMap();
        int code = 2;
        String msg="请先登录！";
        if (users != null) {
            setLog.setlod(httpServletRequest, users.getUsername()+"用户正在修改密码操作",logService);
           if(newpass.equals(newpass2))
           {
               String md5newpass = UtilPacket.Md5MD5String(newpass);
               if (md5newpass.equals(users.getUpassword())){
                   code=1;
                   msg="新密码与旧密码相同，请重新输入！";
               }
               else {
                   users.setUpassword(md5newpass);
                   if(usersService.updateByPrimaryKeySelective(users)>0){
                       code=0;
                       msg="密码修改成功,请重新登录！";
                       session.invalidate();
                   }
               }
           }
           else {
               code=3;
               msg="两次输入密码不相同，请重新输入！";
           }
        }
        map.put("code",code);
        map.put("msg",msg);
        return map;
    }

    /*修改教师信息*/
    @RequestMapping("/upgrxxxg")
    public boolean upgrxxxg(@RequestParam(value = "id") int id,
                            @RequestParam(value = "field") String field,
                            @RequestParam(value = "updateValue") String updateValue,
                            HttpServletRequest httpServletRequest) {
        Teacher teacher = teacherService.getTeacherById(id);
        if (teacher != null) {
            if (field.equals("tname"))
                teacher.setTname(updateValue);
            if (field.equals("tsex"))
                teacher.setTsex(updateValue);
            if (field.equals("tphone"))
                teacher.setTphone(updateValue);
            if (teacherService.upTeacher(teacher) != 0) {
                setLog.setlod(httpServletRequest, "修改了id为" + id + "的教师" + field + "为" + updateValue,logService);
                return true;
            }
            return false;
        }
        return false;
    }

    /*修改用户组信息*/
    @RequestMapping("/upyhzxxgl")
    public boolean upyhzxxgl(@RequestParam(value = "id") int id,
                             @RequestParam(value = "field") String field,
                             @RequestParam(value = "updateValue") String updateValue,
                             HttpServletRequest httpServletRequest) {
        Groupinfo groupinfo = groupinfoService.getGroupinfoById(id);
        if (groupinfo != null) {
            groupinfo.setGroupname(updateValue);
            if (groupinfoService.upGroupinfo(groupinfo) != 0) {
                setLog.setlod(httpServletRequest, "修改了id为" + id + "组信息的名称为" + updateValue,logService);
                return true;
            }
            return false;
        }
        return false;
    }

    /*修改课程比例*/
    @RequestMapping("/upkcbl")
    public boolean upkcbl(@RequestParam(value = "id") int id,
                             @RequestParam(value = "field") String field,
                             @RequestParam(value = "updateValue") String updateValue,
                             HttpServletRequest httpServletRequest) {
        Kcbl kcbl = kcblService.getkcbibyid(id);
        if (kcbl != null) {
            kcbl.setBl(updateValue);
            if (kcblService.upkcbl(kcbl) != 0) {
                setLog.setlod(httpServletRequest, "修改了id为" + id + "的课程比例为" + updateValue,logService);
                return true;
            }
            return false;
        }
        return false;
    }

    /*修改菜单信息*/
    @RequestMapping("/upcdgl")
    public boolean upcdgl(@RequestParam(value = "id") int id,
                          @RequestParam(value = "field") String field,
                          @RequestParam(value = "updateValue") String updateValue,
                          HttpServletRequest httpServletRequest) {
        Menu menu = menuService.selectByPrimaryKey(id);
        if (menu != null) {
            if (field.equals("menuname"))
                menu.setMenuname(updateValue);
            if (field.equals("menulink"))
                menu.setMenulink(updateValue);
            if (menuService.updateByPrimaryKeySelective(menu) != 0) {
                setLog.setlod(httpServletRequest, "修改了" + id + "菜单的" + field + "为" + updateValue,logService);
                return true;
            }
            return false;
        }
        return false;
    }

    /**
     * excel导入学生数据
     * */
    @RequestMapping("/importXS")
    @ResponseBody
    public JSONObject importDataXS(@RequestParam MultipartFile file, HttpServletRequest request) {
        String message = "";
        List beanList = null;
        List spjList = null;
        try {
            // 解析excel
            spjList = importUtilService.loadCcsbFromExcel(file.getInputStream(), 1);

        } catch (Exception e) {
            e.printStackTrace();
            message = "解析数据出错，请检查数据。";
        }

        if (spjList == null || spjList.size() == 0) {
            message = "没有解析到数据，导入0条数据。";
        }

        // 更新数据
        if (spjList != null && spjList.size() > 0) {
            int count = 0;
            String errorXh = "";
            beanList = parseListXS(spjList);
            List list = new ArrayList();
            for (int i = 0; i < beanList.size(); i++) {
                Map beanMap = (Map) beanList.get(i);
                int errCnt = (int)beanMap.get("err");
                if (errCnt != 1) {
                    list.add(beanMap);
                    count++;
                } else if ("".equals(errorXh)) {
                    errorXh = errorXh + (i + 1);
                } else {
                    errorXh = errorXh + "," + (i + 1);
                }
                if (count == beanList.size()) {
                    message = "导入成功，导入" + count + "条数据。";
                } else {
                    message = "导入失败，请检查第" + errorXh + "条数据。";
                }
            }
            if ("".equals(errorXh)) {
                //保存需求上报数据
                for (int m = 0; m < list.size(); m++) {
                    Map bean_tmp = (Map) list.get(m);
                    int xuehao = (int)Double.parseDouble((String) bean_tmp.get("xuehao"));
                    Student student = studentService.getStudentByIdcard(xuehao);
                    if (student==null) {
                        Student student_temp = new Student();
                        student_temp.setSsex((String) bean_tmp.get("ssex"));
                        student_temp.setXuehao(xuehao);
                        student_temp.setSphone((String) bean_tmp.get("sphone"));
                        student_temp.setSscid((int)Double.parseDouble((String) bean_tmp.get("sscid")));
                        String datetimestr = (String) bean_tmp.get("scsny");
                        String datamin[] = datetimestr.split("-");
                        Date date = new Date(Integer.parseInt(datamin[0])-1900,Integer.parseInt(datamin[1]),Integer.parseInt(datamin[2]));
                        student_temp.setScsny(date);
                        student_temp.setSname((String) bean_tmp.get("sname"));
                        int i = studentService.insertStudent(student_temp);
                        if (i!=0) {
                            setLog.setlod(request, "导入了学号为" + xuehao + "的学生数据", logService);
                            Users users = new Users();
                            users.setUpassword(UtilPacket.Md5MD5String("123456"));
                            users.setUsername(String.valueOf(xuehao));
                            users.setGroupid(3);
                            users.setRegtime(new Date(new Date().getTime()));
                            if (usersService.insertSelective(users)!=0){
                                setLog.setlod(request, "创建了用户名为" + xuehao + "的学生用户账号", logService);
                            }
                        }
                    }
                    else
                        continue;
                }
            }
        }
        Map map = new HashMap();
        map.put("message", message);
        JSONObject json = new JSONObject(map);
        return json;
    }

    public List parseListXS(List src) {
        List rslt = new ArrayList();
        for (int i = 0; i < src.size(); i++) {
            Map rowMap = (Map) src.get(i);
            if (rowMap != null) {
                Map bean = parseOneRowXS(rowMap);
                rslt.add(bean);
            }
        }
        return rslt;
    }

    /*获取Excel表格中具体值*/
    private Map parseOneRowXS(Map rowMap) {
        Map resMap = new HashMap();
        resMap.put("err", 0);
        if (rowMap == null) {
            return null;
        }
        for (int j = 0; j <= rowMap.size(); j++) {
            String contentStr = (String) rowMap.get(j);
            if (contentStr == null || contentStr.trim().length() == 0 || "null".equals(contentStr)) {
                //只判断非空字段
                if (j == 0) {
                    resMap.put("err", 1);
                }
                if (j == 1) {
                    resMap.put("err", 1);
                }
                if (j == 2) {
                    resMap.put("err", 1);
                }
                if (j == 3) {
                    resMap.put("err", 1);
                }
                if (j == 4) {
                    resMap.put("err", 1);
                }
                if (j == 5) {
                    resMap.put("err", 1);
                }
                continue;
            }
            contentStr = contentStr.trim();
            System.out.println(j + "=" + contentStr);
            if (j == 0) {// 学号
                resMap.put("xuehao", contentStr);
            }
            if (j == 1) {// 姓名
                resMap.put("sname", contentStr);
            }
            if (j == 2) {// 出生日期
                resMap.put("scsny", contentStr);
            }
            if (j == 3) {// 电话
                resMap.put("sphone", contentStr);
            }
            if (j == 4) {// 所属班级编号
                resMap.put("sscid", contentStr);
            }
            if (j == 5) {// 性别
                resMap.put("ssex", contentStr);
            }
        }
        return resMap;
    }

    /**
     * excel导入教师数据
     * */
    @RequestMapping("/importJS")
    @ResponseBody
    public JSONObject importDataJS(@RequestParam MultipartFile file, HttpServletRequest request) {
        String message = "";
        List beanList = null;
        List spjList = null;
        try {
            // 解析excel
            spjList = importUtilService.loadCcsbFromExcel(file.getInputStream(), 1);

        } catch (Exception e) {
            e.printStackTrace();
            message = "解析数据出错，请检查数据。";
        }

        if (spjList == null || spjList.size() == 0) {
            message = "没有解析到数据，导入0条数据。";
        }

        // 更新数据
        if (spjList != null && spjList.size() > 0) {
            int count = 0;
            String errorXh = "";
            beanList = parseListJS(spjList);
            List list = new ArrayList();
            for (int i = 0; i < beanList.size(); i++) {
                Map beanMap = (Map) beanList.get(i);
                int errCnt = (int)beanMap.get("err");
                if (errCnt != 1) {
                    list.add(beanMap);
                    count++;
                } else if ("".equals(errorXh)) {
                    errorXh = errorXh + (i + 1);
                } else {
                    errorXh = errorXh + "," + (i + 1);
                }
                if (count == beanList.size()) {
                    message = "导入成功，导入" + count + "条数据。";
                } else {
                    message = "导入失败，请检查第" + errorXh + "条数据。";
                }
            }
            if ("".equals(errorXh)) {
                //保存需求上报数据
                for (int m = 0; m < list.size(); m++) {
                    Map bean_tmp = (Map) list.get(m);
                    int idcard = (int)Double.parseDouble((String) bean_tmp.get("tcard"));
                    Teacher teacher = teacherService.getTeacherBytcard(idcard);
                    if (teacher==null) {
                        Teacher teacher_temp = new Teacher();
                        teacher_temp.setTsex((String) bean_tmp.get("tsex"));
                        teacher_temp.setTcard(idcard);
                        teacher_temp.setTphone((String) bean_tmp.get("tphone"));
                        teacher_temp.setTzc((String) bean_tmp.get("tzc"));
                        String datetimestr = (String) bean_tmp.get("tbirth");
                        String datamin[] = datetimestr.split("-");
                        Date date = new Date(Integer.parseInt(datamin[0])-1900,Integer.parseInt(datamin[1]),Integer.parseInt(datamin[2]));
                        teacher_temp.setTbirth(date);
                        teacher_temp.setTname((String) bean_tmp.get("tname"));
                        int i = teacherService.insertTeacher(teacher_temp);
                        if (i!=0) {
                            setLog.setlod(request, "导入了工号为" + idcard + "的教师数据", logService);
                            Users users = new Users();
                            users.setUpassword(UtilPacket.Md5MD5String("123456"));
                            users.setUsername(String.valueOf(idcard));
                            users.setGroupid(2);
                            users.setRegtime(new Date(new Date().getTime()));
                            if (usersService.insertSelective(users)!=0){
                                setLog.setlod(request, "创建了用户名为" + idcard + "的教师用户账号", logService);
                            }
                        }
                    }
                    else
                        continue;
                }
            }
        }
        Map map = new HashMap();
        map.put("message", message);
        JSONObject json = new JSONObject(map);
        return json;
    }

    public List parseListJS(List src) {
        List rslt = new ArrayList();
        for (int i = 0; i < src.size(); i++) {
            Map rowMap = (Map) src.get(i);
            if (rowMap != null) {
                Map bean = parseOneRowJS(rowMap);
                rslt.add(bean);
            }
        }
        return rslt;
    }

    private Map parseOneRowJS(Map rowMap) {
        Map resMap = new HashMap();
        resMap.put("err", 0);
        if (rowMap == null) {
            return null;
        }
        for (int j = 0; j <= rowMap.size(); j++) {
            String contentStr = (String) rowMap.get(j);
            if (contentStr == null || contentStr.trim().length() == 0 || "null".equals(contentStr)) {
                //只判断非空字段
                if (j == 0) {
                    resMap.put("err", 1);
                }
                if (j == 1) {
                    resMap.put("err", 1);
                }
                if (j == 2) {
                    resMap.put("err", 1);
                }
                if (j == 3) {
                    resMap.put("err", 1);
                }
                if (j == 4) {
                    resMap.put("err", 1);
                }
                if (j == 5) {
                    resMap.put("err", 1);
                }
                continue;
            }
            contentStr = contentStr.trim();
            System.out.println(j + "=" + contentStr);
            if (j == 0) {// 工号
                resMap.put("tcard", contentStr);
            }
            if (j == 1) {// 姓名
                resMap.put("tname", contentStr);
            }
            if (j == 2) {// 出生日期
                resMap.put("tbirth", contentStr);
            }
            if (j == 3) {// 电话
                resMap.put("tphone", contentStr);
            }
            if (j == 4) {// 职称
                resMap.put("tzc", contentStr);
            }
            if (j == 5) {// 性别
                resMap.put("tsex", contentStr);
            }
        }
        return resMap;
    }

    /**
     * excel导入成绩数据
     * */
    @RequestMapping("/importCJ")
    @ResponseBody
    public JSONObject importDataCJ(@RequestParam MultipartFile file, HttpServletRequest request) {
        List<Kcbl> kcbls = kcblService.getkcbllist();
        double dybl = 0.3;
        double tybl = 0.1;
        double stbl = 1;
        double zhbl = 0.6;
        DecimalFormat df = new DecimalFormat("#.00");
        if (kcbls!=null){
            for(Kcbl kcbl:kcbls){
                String bl = kcbl.getBl();
                String name = kcbl.getKname();
                if (name.equals("德育")){
                    dybl = Double.parseDouble(bl);
                }
                if (name.equals("体育")){
                    tybl = Double.parseDouble(bl);
                }
                if (name.equals("素拓")){
                    stbl = Double.parseDouble(bl);
                }
                if (name.equals("综合成绩学分绩点")){
                    zhbl = Double.parseDouble(bl);
                }
            }
        }
        String message = "";
        List beanList = null;
        List spjList = null;
        try {
            // 解析excel
            spjList = importUtilService.loadCcsbFromExcel(file.getInputStream(), 1);

        } catch (Exception e) {
            e.printStackTrace();
            message = "解析数据出错，请检查数据。";
        }

        if (spjList == null || spjList.size() == 0) {
            message = "没有解析到数据，导入0条数据。";
        }

        // 更新数据
        if (spjList != null && spjList.size() > 0) {
            int count = 0;
            String errorXh = "";
            beanList = parseListCJ(spjList);
            List list = new ArrayList();
            for (int i = 0; i < beanList.size(); i++) {
                Map beanMap = (Map) beanList.get(i);
                int errCnt = (int)beanMap.get("err");
                if (errCnt != 1) {
                    list.add(beanMap);
                    count++;
                } else if ("".equals(errorXh)) {
                    errorXh = errorXh + (i + 1);
                } else {
                    errorXh = errorXh + "," + (i + 1);
                }
                if (count == beanList.size()) {
                    message = "导入成功，导入" + count + "条数据。";
                } else {
                    message = "导入失败，请检查第" + errorXh + "条数据。";
                }
            }
            if ("".equals(errorXh)) {
                //保存需求上报数据
                for (int m = 0; m < list.size(); m++) {
                    Map bean_tmp = (Map) list.get(m);
                    int sskid = (int)Double.parseDouble((String) bean_tmp.get("sskid"));
                    int sssid = (int)Double.parseDouble((String) bean_tmp.get("sssid"));
                    int st = (int)Double.parseDouble((String) bean_tmp.get("st"));
                    double gradeduo = Double.parseDouble((String) bean_tmp.get("grade"));
                    Kecheng kecheng = kechengService.getKechengById(sskid);
                    if (kecheng==null)
                        continue;
                    int xf = kecheng.getXf();
                    String xfjd;
                    if(gradeduo>=60)
                     xfjd = String.valueOf((gradeduo-50)/10);
                    else xfjd=String.valueOf(0);
                    String kname = kecheng.getKname();
                    Grade grade = gradeService.getGradeByKandS(bean_tmp);
                    if (grade==null) {
                        Grade grade_temp = new Grade();
                        grade_temp.setSskid(sskid);
                        grade_temp.setSssid(sssid);
                        grade_temp.setSt(st);
                        String gradestr = (String) bean_tmp.get("grade");
                        grade_temp.setKhfs((String) bean_tmp.get("khfs"));
                        grade_temp.setKsxz((String) bean_tmp.get("ksxz"));
                        grade_temp.setGrade(gradestr);
                        grade_temp.setXfjd(xfjd);
                        if (!kname.equals("素拓")||!kname.equals("德育")||!kname.equals("体育"))
                            grade_temp.setXfjd(xfjd);
                        if (gradeService.insertGrade(grade_temp)!=0)
                            setLog.setlod(request, "导入了课程号为"+sskid+"，学期编号为"+sssid+"，学生编号为"+st+"的成绩",logService);
                        //开始判断是否有该学生本学期综合成绩记录
                        Map sinfomap = new HashMap();
                        sinfomap.put("ssuid",st);
                        sinfomap.put("st",st);
                        sinfomap.put("sssid",sssid);
                        List<Grade> grades = gradeService.getGradeList(sinfomap);
                        Zhcj zhcj = zhcjService.getZhcjByuands(sinfomap);
                        //以下为存在此学生成绩的情况进行更新
                        if (zhcj!=null) {
                            if (kname.equals("素拓"))
                                zhcj.setZhcj(String.valueOf(Double.parseDouble(zhcj.getZhcj()) + Double.parseDouble(gradestr) * stbl));
                            else if (kname.equals("德育")) {
                                zhcj.setZhcj(String.valueOf(Double.parseDouble(zhcj.getZhcj()) + Double.parseDouble(gradestr) * dybl));
                                zhcj.setYscj(String.valueOf(Double.parseDouble(zhcj.getYscj()) + Double.parseDouble(gradestr) * dybl));
                            } else if (kname.equals("体育")) {
                                zhcj.setZhcj(String.valueOf(Double.parseDouble(zhcj.getZhcj()) + Double.parseDouble(gradestr) * tybl));
                                zhcj.setYscj(String.valueOf(Double.parseDouble(zhcj.getYscj()) + Double.parseDouble(gradestr) * tybl));
                            } else {
                                double stcj=0;
                                double dycj=0;
                                double tycj=0;
                                double zcj=0;
                                double zxf=0;
                                int dkxf=0;
                                if (grades!=null){
                                    for (Grade grade2:grades){
                                        int kid = grade2.getSskid();
                                        Kecheng kecheng2 = kechengService.getKechengById(kid);
                                        String knamestr = "已删除该课程";
                                        if (kecheng2!=null) {
                                            knamestr = kecheng2.getKname();
                                            dkxf = kecheng2.getXf();
                                        if (knamestr.equals("素拓")){
                                            stcj = Double.parseDouble(grade2.getGrade());
                                        }
                                        if (knamestr.equals("德育")){
                                            dycj = Double.parseDouble(grade2.getGrade());
                                        }
                                        if (knamestr.equals("体育")){
                                            tycj = Double.parseDouble(grade2.getGrade());
                                        }
                                        if (knamestr.equals("已删除该课程")){
                                            continue;
                                        }
                                         zcj=zcj+Double.parseDouble(grade2.getGrade())*dkxf;
                                            zxf=zxf+dkxf;
                                        }
                                    }
                                }
                                double zxfjd=0;
                                if (zxf!=0)
                                    zxfjd=zcj/zxf;
                                zhcj.setZhcj(df.format(zxfjd*zhbl+dycj*dybl+tycj*tybl+stcj*stbl));
                                zhcj.setYscj(df.format(zxfjd*zhbl+dycj*dybl+tycj*tybl));
                                zhcj.setZxfjd(df.format(zxfjd));
                            }
                            if (zhcjService.upZhcj(zhcj) != 0)
                                setLog.setlod(request, "学生" + st + "的综合成绩已更新", logService);
                        }
                        //如果没有此学生则创建
                        else {
                            Zhcj newzhcj = new Zhcj();
                            newzhcj.setSssid(sssid);
                            newzhcj.setSsuid(st);
                            newzhcj.setSszid(1);
                            if (kname.equals("素拓"))
                                newzhcj.setZhcj(String.valueOf(Double.parseDouble(gradestr) * stbl));
                            else if (kname.equals("德育")) {
                                newzhcj.setZhcj(String.valueOf(Double.parseDouble(gradestr) * dybl));
                                newzhcj.setYscj(String.valueOf(Double.parseDouble(gradestr) * dybl));
                            } else if (kname.equals("体育")) {
                                newzhcj.setZhcj(String.valueOf(Double.parseDouble(gradestr) * tybl));
                                newzhcj.setYscj(String.valueOf(Double.parseDouble(gradestr) * tybl));
                            } else {
                                double stcj=0;
                                double dycj=0;
                                double tycj=0;
                                double zcj=0;
                                double zxf=0;
                                int dkxf=0;
                                if (grades!=null){
                                    for (Grade grade2:grades){
                                        int kid = grade2.getSskid();
                                        Kecheng kecheng2 = kechengService.getKechengById(kid);
                                        String knamestr = "已删除该课程";
                                        if (kecheng2!=null) {
                                            knamestr = kecheng2.getKname();
                                            dkxf = kecheng2.getXf();
                                        if (knamestr.equals("素拓")){
                                            stcj = Double.parseDouble(grade2.getGrade());
                                        }
                                        else if (knamestr.equals("德育")){
                                            dycj = Double.parseDouble(grade2.getGrade());
                                        }
                                        else if (knamestr.equals("体育")){
                                            tycj = Double.parseDouble(grade2.getGrade());
                                        }
                                        else if (knamestr.equals("已删除该课程")){
                                            continue;
                                        }
                                        else {
                                            zcj=zcj+Double.parseDouble(grade2.getGrade())*dkxf;
                                            zxf=zxf+dkxf;
                                        }
                                        }
                                    }
                                }
                                double zxfjd=0;
                                if (zxf!=0)
                                    zxfjd=zcj/zxf;
                                newzhcj.setZhcj(df.format(zxfjd*zhbl+dycj*dybl+tycj*tybl+stcj*stbl));
                                newzhcj.setYscj(df.format(zxfjd*zhbl+dycj*dybl+tycj*tybl));
                                newzhcj.setZxfjd(df.format(zxfjd));
                            }
                            if (zhcjService.insertZhcj(newzhcj)!=0)
                                setLog.setlod(request, "学生" + st + "的综合成绩已创建", logService);
                        }
                        }
                       else
                           continue;
                    }
                }
            }

        Map map = new HashMap();
        map.put("message", message);
        JSONObject json = new JSONObject(map);
        return json;
    }

    public List parseListCJ(List src) {
        List rslt = new ArrayList();
        for (int i = 0; i < src.size(); i++) {
            Map rowMap = (Map) src.get(i);
            if (rowMap != null) {
                Map bean = parseOneRowCJ(rowMap);
                rslt.add(bean);
            }
        }
        return rslt;
    }

    private Map parseOneRowCJ(Map rowMap) {
        Map resMap = new HashMap();
        resMap.put("err", 0);
        if (rowMap == null) {
            return null;
        }
        for (int j = 0; j <= rowMap.size(); j++) {
            String contentStr = (String) rowMap.get(j);
            if (contentStr == null || contentStr.trim().length() == 0 || "null".equals(contentStr)) {
                //只判断非空字段
                if (j == 0) {
                    resMap.put("err", 1);
                }
                if (j == 1) {
                    resMap.put("err", 1);
                }
                if (j == 2) {
                    resMap.put("err", 1);
                }
                if (j == 3) {
                    resMap.put("err", 1);
                }
                if (j == 4) {
                    resMap.put("err", 1);
                }
                if (j == 5) {
                    resMap.put("err", 1);
                }
                continue;
            }
            contentStr = contentStr.trim();
            System.out.println(j + "=" + contentStr);
            if (j == 0) {// 所属课程编号
                resMap.put("sskid", contentStr);
            }
            if (j == 1) {// 所属学期编号
                resMap.put("sssid", contentStr);
            }
            if (j == 2) {// 考核方式
                resMap.put("khfs", contentStr);
            }
            if (j == 3) {// 考试性质
                resMap.put("ksxz", contentStr);
            }
            if (j == 4) {// 所属学生编号
                resMap.put("st", contentStr);
            }
            if (j == 5) {// 分数
                resMap.put("grade", contentStr);
            }
        }
        return resMap;
    }
}
